﻿#include "GUICurvePlotDialog.h"
#include "ui_GUICurvePlotDialog.h"
#include "GUIDialog/GUIPhyscisDialog/QwtCustomPlot.h"
#include "FITK_Kernel/FITKCore/FITKAbstractGUI.h"
#include <qwt_scale_engine.h>
#include <qwt_plot_curve.h>
#include <QList>
#include <QPointF>
namespace GUI
{
    GUICurvePlotDialog::GUICurvePlotDialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::GUICurvePlotDialog)
    {
        ui->setupUi(this);
        // 去掉帮助按钮
        setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
        initPlot();
        setWindowTitle(tr("PlotCurve"));
        ui->tableWidgetCurveData->setEditTriggers(QAbstractItemView::NoEditTriggers);
    }

    GUICurvePlotDialog::~GUICurvePlotDialog()
    {
        delete ui;
    }

  

    void GUICurvePlotDialog::addCurve(QVector<QPointF> data, QString name /*= "Curve"*/)
    {
      
    }

    void GUICurvePlotDialog::addCurve(QVector<QPointF> data, QColor color, QString name)
    {
        _qwtCustomPlot->addCurve(data, color, name);
        _qwtCustomPlot->updateAllLegendChecked();
    }

    void GUICurvePlotDialog::addCurve(QVector<float> xvec, QVector<float> yvec, QColor color, QString name)
    {
        _qwtCustomPlot->setAxisName("Frequency(HZ)","");
        _qwtCustomPlot->addCurve(xvec, yvec,color, name);
        _qwtCustomPlot->updateAllLegendChecked();
    }

    void GUICurvePlotDialog::clearCurves()
    {
        _qwtCustomPlot->clearCurves();
    }

    void GUICurvePlotDialog::loadTableData(QHash<QString, QVector<float>> &yHash, QList<QString> colNameList, int rowCount)
    {
        _yColDatas = yHash;
        _colKeyList = colNameList;
        //初始化表头信息
        tableInit(ui->tableWidgetCurveData, _colKeyList, colNameList.count(),rowCount);
        int icolindex=0;
        for (QString keyname : _colKeyList)
        {
            QVector<float> ydata = _yColDatas.value(keyname);
            //给表按列赋数据
            setTableValueByCol(ui->tableWidgetCurveData, ydata, icolindex);
            icolindex++;
        }
    }

    void GUICurvePlotDialog::setCaseNames(QStringList caseNames, QStringList datasetNames)
    {
        //更新工况和数据集列表
        _dataSetNameList = datasetNames;
        _caseNameList = caseNames;
        ui->comboBoxCaseList->clear();
        ui->comboBoxCaseList->addItems(caseNames);
        ui->comboBoxDataList->clear();
        ui->comboBoxDataList->addItems(datasetNames);
    }

    void GUICurvePlotDialog::on_comboBoxCaseList_currentIndexChanged(int index)
    {
        Q_UNUSED(index);
        //给操作器发送工况和数据集名称数据
        sendUpdateData();
    }

    void GUICurvePlotDialog::on_comboBoxDataList_currentIndexChanged(int index)
    {
        Q_UNUSED(index);
        //给操作器发送工况和数据集名称数据
        sendUpdateData();
    }

    void GUICurvePlotDialog::sendUpdateData()
    {
        QString caseName = ui->comboBoxCaseList->currentText();
        QString datasetName = ui->comboBoxDataList->currentText();
        emit caseChangedSignal(caseName, datasetName);
    }

    void GUICurvePlotDialog::initPlot()
    {
        _qwtCustomPlot = new QwtCustomPlot();
        ui->verticalLayout->addWidget(_qwtCustomPlot);
        _qwtCustomPlot->initLegend();
        _qwtCustomPlot->setAxisScaleEngine(QwtPlot::xBottom, new QwtLogScaleEngine);
        _qwtCustomPlot->setAxisScaleEngine(QwtPlot::yLeft, new QwtLogScaleEngine);
    }
    QString GUICurvePlotDialog::valueToString(double iValue)
    {
        return QString::number(iValue);
    }

   
    void GUICurvePlotDialog::tableInit(QTableWidget* table, QStringList title, int col, int row /*= 1*/, QString defaultValue /*= "0.0"*/)
    {
        table->setRowCount(row);
        table->setColumnCount(col);
        table->setHorizontalHeaderLabels(title);
        for (int rowIndex = 0; rowIndex < row; rowIndex++)
        {
            for (int colIndex = 0; colIndex < col; colIndex++)
            {
                if (table->item(rowIndex, colIndex) == nullptr)
                {
                    table->setItem(rowIndex, colIndex, new QTableWidgetItem(defaultValue));
                }
            }
        }
    }
    bool GUICurvePlotDialog::setTableValueByCol(QTableWidget* table, QVector<float> list, int col /*= 0*/)
    {
        bool noError = true;
        int tableRow = table->rowCount();
        int tableCol = table->columnCount();
        int size = list.size();
        int min = qMin(size, tableRow);

        if (col < tableCol)
        {
            for (int i = 0; i < min; i++)
            {
                auto item = table->item(i, col);
                if (item)
                {
                   //给表格赋值
                    item->setText(valueToString(list.at(i)));
                }
                else
                {
                    noError = false;
                }
            }
        }
        return noError;
    }
}

